// pages/personal/personal.js
const request = require('../../utils/promise.js');
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    avatarUrl: defaultAvatarUrl,
  },

  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    console.log('avatarUrl = ' + avatarUrl)
    this.setData({
      avatarUrl,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.reqUserInfoData();
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '我'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '我'
    }
  },

  /**
   * 请求获取用户信息
   */
  reqUserInfoData() {
    request.GET('/user/userInfo', {openId: wx.getStorageSync('openId')}).then(res => {
      // console.log(res)
      if (res.data.resCode==200) {
        this.setData({
          userInfo: res.data.userInfo
        })
        wx.setStorageSync('userId', res.data.userInfo['id']); // 缓存用户 id
      } else {
        wx.showToast({
          title: '获取数据异常',
          icon: 'error'
        })
      }
    })
  },

  /**
   * 跳转修改昵称
   */
  gotoModifyUsername() {
    if (this.data.userInfo['times']==0) {
      wx.navigateTo({
        url: '/pages/personal_edit/personal_edit?type=0',
      })
    }
  },

  /**
   * 跳转修改 兴趣爱好, 教育经历等
   */
  gotoModifyOtherInfo(e) {
    wx.navigateTo({
      url: '/pages/personal_edit/personal_edit?type='+ e.currentTarget.dataset.type,
    })
  },

  /**
   * 查看更多个人信息
   */
  clickViewMoreDetail() {
    wx.navigateTo({
      url: '/pages/personal_detail/personal_detail',
    })
  },

  /**
   * 点击查看我的工作技能
   */
  clickViewMyWorkSkill() {
    wx.navigateTo({
      url: '/pages/work_skill/work_skill?userId='+ this.data.userInfo['id'],
    })
  },

  /**
   * 点击申请成为公社社员
   */
  clickApplyMembership() {
    wx.navigateTo({
      url: '/pages/membership_apply/membership_apply',
    })
  },

})